﻿@{
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
    <input type="text" id="id" value="@ViewBag.id" hidden />
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="text"
                   name="roleName"
                   lay-verify="required"
                   placeholder="请输入"
                   autocomplete="off"
                   class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="checkbox"
                   name="status"
                   lay-skin="switch"
                   lay-filter="switchTest"
                   title="启用|禁用" />
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="add">
                保存
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script>
    //关闭弹框
    function closeWorkStation() {
        var index = parent.layer.getFrameIndex(window.name);//先得到当前的索引
        parent.layer.close(index);//再执行关闭
    }

    function getAccount () {
        var id = $("#id").val();
        if(id != 0){
            $.ajax({
                url:'/Role/GetSingRole?id='+id,
                type:'get',
                dataType:'json',
                success:function(res){
                    $('[name="roleName"]').val(res.roleName);
                    $('[name="status"]').prop('checked',res.status);
                    layui.form.render('checkbox')
                }
            })
        }
    }

    layui.use(['form', 'laydate', 'util'], function () {
      var form = layui.form;
      var layer = layui.layer;

      getAccount()


      // 提交事件
      form.on('submit(add)', function (data) {
        var field = data.field; // 获取表单字段值
        field.id = $("#id").val();
        field.status = field.status == "on";
        console.log(field);
        if(field.id == 0){
          $.ajax({
            url:'/Role/RoleAdd',
            data:field,
            type:'post',
            dataType:'text',
            success:function(res){
                if(res>0){
                    layer.msg('新增成功', { icon: 1 });
                    setTimeout(function(){closeWorkStation()},330)
                }
                else{
                    layer.msg('新增失败', { icon: 0 });
                }
            }
          })
        }
        else{
          $.ajax({
            url:'/Role/RoleUpd',
            data:field,
            type:'post',
            dataType:'text',
            success:function(res){
                if(res>0){
                    layer.msg('编辑成功', { icon: 1 });
                    setTimeout(function(){closeWorkStation()},330)
                }
                else{
                    layer.msg('编辑失败', { icon: 0 });
                }
            }
          })
        }
        return false; // 阻止默认 form 跳转
      });
    });
</script>